﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Wizzard
{

    class Fogo
    {
        Texture2D textura;
        int vel = 5;
        public Rectangle posicao;
        public static List<Fogo> tiros = new List<Fogo>();
        int tempo;
        int pos;
        int tipoTiro;

        public Fogo()
        {
        }
        
        public Fogo(ContentManager content, GameWindow window, int dificuldade)
        {
            Random random;
            random = new Random();
            pos = random.Next(1, 4);
            tipoTiro = 1;
            if (dificuldade == 1)
            {
                tipoTiro = random.Next(1, 3);
            }
            textura = content.Load<Texture2D>("Fogo");
            if (tipoTiro == 2) textura = content.Load<Texture2D>("Fogo2");
            if (pos == 1)
            {
                posicao.X = window.ClientBounds.Width + textura.Width;
                posicao.Y = random.Next(275, 351);

            }
            if (pos == 2)
            {
                posicao.X = - textura.Width;
                posicao.Y = random.Next(275, 351);

            }
            if (pos == 3)
            {
                posicao.X = random.Next(0, window.ClientBounds.Width + 1);
                posicao.Y = -textura.Height;
            }            
            posicao.Width = textura.Width;
            posicao.Height = textura.Height;
        }

        public void Update(GameTime gameTime, GameWindow window)
        {
            for (int i = 0; i < tiros.Count; i++)
            {
                if (tiros[i].tipoTiro == 1)
                {
                    if (tiros[i].pos == 1) tiros[i].posicao.X -= vel;
                    if (tiros[i].pos == 2) tiros[i].posicao.X += vel;
                    if (tiros[i].pos == 3) tiros[i].posicao.Y += vel;
                }
                if (tiros[i].tipoTiro == 2)
                {
                    if (tiros[i].pos == 1) tiros[i].posicao.X -= vel + 2;
                    if (tiros[i].pos == 2) tiros[i].posicao.X += vel + 2;
                    if (tiros[i].pos == 3) tiros[i].posicao.Y += vel + 2;
                }
                if (tiros[i].posicao.X < -tiros[i].textura.Width * 2)
                {
                    tiros.RemoveAt(i);
                }
                if (tiros[i].posicao.X > window.ClientBounds.Width + tiros[i].textura.Width * 2)
                {
                    tiros.RemoveAt(i);
                }
                if (tiros[i].posicao.Y < -tiros[i].textura.Width * 2)
                {
                    tiros.RemoveAt(i);
                }
                if (tiros[i].posicao.Y > window.ClientBounds.Height + tiros[i].textura.Width * 2)
                {
                    tiros.RemoveAt(i);
                }
            }            
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            tempo = (int)(gameTime.TotalGameTime.TotalSeconds * 4) % 2;
            for (int i = 0; i < tiros.Count; i++)
            {
                if (tiros[i].tipoTiro == 1) spriteBatch.Draw(tiros[i].textura, tiros[i].posicao, Color.White);
                if (tiros[i].tipoTiro == 2) spriteBatch.Draw(tiros[i].textura, tiros[i].posicao, Color.White);
            }
        }

        public void RemoverTiros()
        {
            for (int i = 0; i < tiros.Count; i++)
            {
                tiros.RemoveAt(0);
            }

        }

    }

}
